package com.workingpub.commons.orm.internal;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.DiscriminatorType;
import javax.persistence.InheritanceType;

import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
 * 实体JPA注解元信息
 * 
 * @author taoping
 *
 */
public class EntityMetaInfo {
	private String tableName;
	private boolean cache = Boolean.FALSE.booleanValue();
	/**
	 * Key is column name, value is a {@link Field} or
	 * {@link PropertyDescriptor}
	 */
	private Map<String, Object> columnInfos;
	private InheritanceType inheritanceType;
	private String discriminatorColumn;
	private DiscriminatorType discriminatorType;
	private Object discriminatorValue;
	private String primaryKeyJoinColumn;
	private String idname;
	private String className;

	private EntityMetaInfo parent;

	public EntityMetaInfo() {

	}

	protected void setParent(EntityMetaInfo parent) {
		this.parent = parent;
	}

	public EntityMetaInfo getParent() {
		return parent;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getIdname() {
		return idname;
	}

	public void setIdname(String idname) {
		this.idname = idname;
	}

	public boolean isCache() {
		return cache;
	}

	public void setCache(boolean cache) {
		this.cache = cache;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	/**
	 * 
	 * Key is column name (UpperCase), value is a {@link Field} or
	 * {@link PropertyDescriptor}
	 * 
	 * @return
	 */
	public Map<String, Object> getColumnInfos() {
		return columnInfos;
	}

	public void addColumnInfo(String name, Field value) {
		putColumnInfo(name, value);
	}

	public void addColumnInfo(String name, PropertyDescriptor value) {
		putColumnInfo(name, value);
	}

	private void putColumnInfo(String name, Object value) {
		if (StringUtils.isEmpty(name)) {
			return;
		}
		if (null == columnInfos) {
			columnInfos = new HashMap<String, Object>();
		}
		Assert.isTrue(!columnInfos.containsKey(name), "The column name is repetition : " + name);
		columnInfos.put(name, value);
	}

	/**
	 * Ignore Case
	 * 
	 * @param key
	 * @return
	 */
	public boolean containsColumnKey(String key) {
		if (!StringUtils.hasText(key) || null == this.columnInfos || this.columnInfos.isEmpty()) {
			return false;
		}
		for (String item : this.columnInfos.keySet()) {
			if (item.equalsIgnoreCase(key)) {
				return true;
			}
		}
		return false;
	}

	public InheritanceType getInheritanceType() {
		return inheritanceType;
	}

	public void setInheritanceType(InheritanceType inheritanceType) {
		this.inheritanceType = inheritanceType;
	}

	public String getDiscriminatorColumn() {
		return discriminatorColumn;
	}

	public void setDiscriminatorColumn(String discriminatorColumn) {
		this.discriminatorColumn = discriminatorColumn;
	}

	public DiscriminatorType getDiscriminatorType() {
		return discriminatorType;
	}

	public void setDiscriminatorType(DiscriminatorType discriminatorType) {
		this.discriminatorType = discriminatorType;
	}

	public Object getDiscriminatorValue() {
		return discriminatorValue;
	}

	public void setDiscriminatorValue(Object discriminatorValue) {
		this.discriminatorValue = discriminatorValue;
	}

	public String getPrimaryKeyJoinColumn() {
		return primaryKeyJoinColumn;
	}

	public void setPrimaryKeyJoinColumn(String primaryKeyJoinColumn) {
		this.primaryKeyJoinColumn = primaryKeyJoinColumn;
	}

}
